package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppPackageTwoBo;
import com.ruoyi.app.domain.vo.AppPackageTwoVo;
import com.ruoyi.app.domain.AppPackageTwo;
import com.ruoyi.app.mapper.AppPackageTwoMapper;
import com.ruoyi.app.service.IAppPackageTwoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 爬塔层数Service业务层处理
 *
 * @author heise
 * @date 2025-06-27
 */
@RequiredArgsConstructor
@Service
public class AppPackageTwoServiceImpl implements IAppPackageTwoService {

    private final AppPackageTwoMapper baseMapper;

    /**
     * 查询爬塔层数
     */
    @Override
    public AppPackageTwoVo queryById(Long packageTwoId) {
        return baseMapper.selectVoById(packageTwoId);
    }

    @Override
    public AppPackageTwoVo queryByUp(Long upId) {
        LambdaQueryWrapper<AppPackageTwo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppPackageTwo::getUpId, upId);
        return baseMapper.selectVoOne(wrapper);
    }

    /**
     * 查询爬塔层数列表
     */
    @Override
    public TableDataInfo<AppPackageTwoVo> queryPageList(AppPackageTwoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppPackageTwo> lqw = buildQueryWrapper(bo);
        Page<AppPackageTwoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询爬塔层数列表
     */
    @Override
    public List<AppPackageTwoVo> queryList(AppPackageTwoBo bo) {
        LambdaQueryWrapper<AppPackageTwo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppPackageTwo> buildQueryWrapper(AppPackageTwoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppPackageTwo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPackageTwoId() != null, AppPackageTwo::getPackageTwoId, bo.getPackageTwoId());
        lqw.eq(bo.getUpId() != null, AppPackageTwo::getUpId, bo.getUpId());
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), AppPackageTwo::getPackageName, bo.getPackageName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AppPackageTwo::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增爬塔层数
     */
    @Override
    public Boolean insertByBo(AppPackageTwoBo bo) {
        AppPackageTwo add = BeanUtil.toBean(bo, AppPackageTwo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPackageTwoId(add.getPackageTwoId());
        }
        return flag;
    }

    /**
     * 修改爬塔层数
     */
    @Override
    public Boolean updateByBo(AppPackageTwoBo bo) {
        AppPackageTwo update = BeanUtil.toBean(bo, AppPackageTwo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppPackageTwo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除爬塔层数
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
